package com.mgm.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.mgm.exception.CustomException;
import com.mgm.page.DataGrid;
import com.mgm.pojo.User;
import com.mgm.result.IMessage;
import com.mgm.result.IResult;
import com.mgm.user.service.UserService;


/**
 * 用户Controller
 * <p>Title: UserController</p>
 * <p>Description: </p>
 * <p>Company: MGM</p> 
 * @author	Andy
 * @date	2018年6月20日上午10:52:43
 * @version 1.0
 */
@Controller
@RequestMapping("/users")
public class UserController {
	
	private static Logger logger =LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;
	
	/**
	 * 
	 * @Title: listUsers   
	 * @Description: 获取用户   
	 * @param: @param pageNum
	 * @param: @param pageSize
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<DataGrid>>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<IResult<DataGrid>> listUsers(@RequestParam(value="page", defaultValue="1") Integer pageNum,
			@RequestParam(value="ps", defaultValue="10") Integer pageSize, HttpServletRequest request, HttpServletResponse response) {
		DataGrid dg = userService.findAll(pageNum, pageSize);
		IResult<DataGrid> result = IResult.success(dg);
		
		return new ResponseEntity<IResult<DataGrid>>(result, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: createUser  
	 * @Description: 添加用户  
	 * @param: @param user
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.POST) 
	public ResponseEntity<IMessage> createUser(@RequestBody User user){
		List<User> list = userService.findByName(user.getName());
		if(!CollectionUtils.isEmpty(list)) {
			throw new CustomException("用户名已存在");
		}
		IMessage msg= userService.addUser(user);
		return new ResponseEntity<IMessage>(msg, HttpStatus.CREATED);
	}
	
	/**
	 * 
	 * @Title: getUser   
	 * @Description: 获取用户详情   
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<User>>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResponseEntity<IResult<User>> getUser(@PathVariable("id") Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		User user = userService.selectById(id);
		IResult<User> result = IResult.success(user);
		return new ResponseEntity<IResult<User>>(result, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: updateUser   
	 * @Description: 更新用户  
	 * @param: @param user
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> updateUser(@RequestBody User user){
		if(user.getId()==null || user.getId() <= 0) {
			logger.error("Illegal operation, User update fail!");
			throw new CustomException("非法操作，更新失败");
		}
		User u = userService.selectById(user.getId());
		if(u==null) {
			logger.error("Unable to update. User with id " + user.getId() + " not found");
			throw new CustomException("用户不存在，更新失败");
		}
		IMessage msg = userService.updateUser(user);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: deleteUser   
	 * @Description: 删除用户
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 
	public ResponseEntity<IMessage> deleteUser(@PathVariable("id") Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		User user = userService.selectById(id);
		if(user==null) {
			logger.error("Unable to delete. User with id " + id + " not found");
			throw new CustomException("用户不存在，删除失败");
		}
		IMessage msg= userService.deleteUser(user);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: enabled   
	 * @Description: 启用用户   
	 * @param: @param id
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}/enabled", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> enabled(@PathVariable("id") Integer id, 
			@RequestBody Map<String, Boolean> map, 
			HttpServletRequest request, HttpServletResponse response) {
		User user = userService.selectById(id);
		if(user==null) {
			logger.error("Unable to enabled. User with id " + id + " not found");
			throw new CustomException("用户不存在，操作失败");
		}
		if(map==null || map.get("enabled")==null) {
			logger.error("[Enabled User] Illegal operation");
			throw new CustomException("非法操作");
		}
		
		IMessage msg= userService.isEnabled(id, map.get("enabled"));
		
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
}
